package com.itheima.health.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.health.dao.RoleDao;
import com.itheima.health.dao.RolePermissionDao;
import com.itheima.health.dao.UserRoleDao;
import com.itheima.health.entity.PageResult;
import com.itheima.health.entity.QueryPageBean;
import com.itheima.health.pojo.Role;
import com.itheima.health.pojo.RolePermission;
import com.itheima.health.service.RoleService;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;

@DubboService
public class RoleServiceImpl implements RoleService {

    @Autowired
    private RoleDao roleDao;

    @Autowired
    private UserRoleDao userRoleDao;

    @Autowired
    private RolePermissionDao rolePermissionDao;

    @Override
    public PageResult findPage(QueryPageBean queryPageBean) {
        PageHelper.startPage(queryPageBean.getCurrentPage(),queryPageBean.getPageSize());
        Page pages = roleDao.findPage(queryPageBean.getQueryString());
        PageResult pageResult = new PageResult();
        pageResult.setTotal(pages.getTotal());
        pageResult.setRows(pages.getResult());
        return pageResult;
    }

    @Override
    public List all() {
        List list = roleDao.selectAll();
        return list;
    }

    @Override
    public List findByUserRoleId(Integer id) {
        List byUserRoleId = userRoleDao.findByUserRoleId(id);
        return byUserRoleId;
    }

    @Override
    public int add(Role role, Integer[] permissions) {
        int i = roleDao.insertSelective(role);
        Role byName = roleDao.findByName(role.getName());
        for (Integer permission : permissions) {
            RolePermission rolePermission = new RolePermission();
            rolePermission.setPermissionId(permission);
            rolePermission.setRoleId(byName.getId());
            rolePermissionDao.insertSelective(rolePermission);
        }
        return i;
    }

    @Override
    public Role findById(Integer id) {
        Role role = roleDao.selectByPrimaryKey(id);
        return role;
    }

    @Override
    public List findByRolePermission(Integer id) {
        List byRolePermissionId = rolePermissionDao.findByRolePermissionId(id);
        return byRolePermissionId;
    }

    @Override
    public int edit(Role role, Integer[] permissions) {
        int i = roleDao.updateByPrimaryKey(role);
        rolePermissionDao.deleteByRoleId(role.getId());
        for (Integer permission : permissions) {
            RolePermission rolePermission = new RolePermission();
            rolePermission.setRoleId(role.getId());
            rolePermission.setPermissionId(permission);
            rolePermissionDao.insertSelective(rolePermission);
        }
        return i;
    }

    @Override
    public int delete(Integer id) {
        rolePermissionDao.deleteByRoleId(id);
        int i = roleDao.deleteById(id);
        return i;
    }
}
